using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

namespace Edge.Common.Utils
{
    public static class FileUtils
    {
        /// <summary>
        /// copy directory contents
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="destDir"></param>
        public static void CopyDirectory(string sourceDir /*sourceDir*/, string destDir /*destDir*/)
        {
            // code copied from http://www.codeproject.com/cs/files/copydirectoriesrecursive.asp

            String[] files;

            if (destDir[destDir.Length - 1] != Path.DirectorySeparatorChar)
                destDir += Path.DirectorySeparatorChar;

            if (!Directory.Exists(destDir)) Directory.CreateDirectory(destDir);
            files = Directory.GetFileSystemEntries(sourceDir);
            foreach (string element in files)
            {
                // Sub directories
                if (Directory.Exists(element))
                    CopyDirectory(element, destDir + Path.GetFileName(element));
                // Files in directory
                else
                    File.Copy(element, destDir + Path.GetFileName(element), true);
            }
        }

        public static void UploadImg(HttpPostedFile file, string directory, string filename)
        {
            UploadImg(file, directory, filename, string.Empty);
        }

        public static void UploadImg(HttpPostedFile file, string directory, string filename, string watermarkPath)
        {
            if (file != null)
            {
                if (file.InputStream != null && file.InputStream.Length != 0)
                {
                    Byte[] datafile = new byte[file.InputStream.Length];
                    file.InputStream.Read(datafile, 0, (int)file.InputStream.Length);
                    file.InputStream.Close();
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    if (watermarkPath != string.Empty)
                    {
                        File.WriteAllBytes(directory + "tmp_" +filename, datafile);
                        AddWidth(directory + "tmp_" + filename, directory + filename);
                        File.Delete(directory + "tmp_" + filename);
//                        SetWatermark(directory + "temp_" + filename, watermarkPath, directory + filename);
//                        File.Delete(directory + "temp_" + filename);
                        GetThumbnail(directory,filename,"thumb_",175,0);
                       
                    }
                    else
                    {
                        File.WriteAllBytes(directory + filename, datafile);
                        
                    }
                    
                }
            }
            return;
        }

        public static void SetWatermark(string filePath, string watermarkPath, string resultPath)
        {
            
            Image original = Image.FromFile(filePath);
            Image watermark = Image.FromFile(watermarkPath);
            Graphics gra = Graphics.FromImage(original);
            gra.DrawImage(watermark, 10, original.Height - 30 );
            original.Save(resultPath);
            original.Dispose();
            gra.Dispose();
            watermark.Dispose();
        }
        public static void AddWidth(string filePath, string resultPath)
        {
            Image original = Image.FromFile(filePath);
            if (original.Width < original.Height)
            {
                int widthTmp = original.Height + (original.Height - original.Width) / 2;
                int heightTmp = original.Height;
                Graphics settings = Graphics.FromImage(original);
                Bitmap bitmap = new Bitmap(widthTmp, heightTmp, settings);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                using (Brush brush = new SolidBrush(Color.White))
                {
                    graphics.FillRectangle(brush, 0, 0, widthTmp, heightTmp);
                }
                Graphics gra = Graphics.FromImage(bitmap);
                gra.DrawImage(original, (widthTmp - original.Width) / 2, 0);
                EncoderParameters parameters = new EncoderParameters(1);
                parameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)90);
                bitmap.Save(resultPath, GetCodecInfo("image/jpeg"), parameters);
                bitmap.Dispose();
                settings.Dispose();
                gra.Dispose();
            }
            else
            {
                original.Save(resultPath);
            }
            original.Dispose();
        }

        public static void GetThumbnail(string directory, string filename, string thumbName, int width, int height)
        {
            Image image = Image.FromFile(directory + filename);
            
            int thumbWidth = width;
            int thumbHeight = height;
            if (width == 0)
            {
                thumbWidth = image.Width * height / image.Height;
            }
//            else if (width < image.Width)
//            {
//                thumbWidth = image.Width;
//            }
            if (height == 0)
            {
                thumbHeight = image.Height * width / image.Width;
            }
//            else if (height < image.Height)
//            {
//                thumbHeight = image.Height;
//            }
            
            Image thumbnailImage = image.GetThumbnailImage(thumbWidth, thumbHeight, ThumbnailCallback, IntPtr.Zero);
            
            if (File.Exists(directory + thumbName + filename))
            {
                File.Delete(directory + thumbName + filename);
            }
            thumbnailImage.Save(directory + thumbName + filename);//, GetCodecInfo("image/jpeg"), parameters);
            image.Dispose();
            thumbnailImage.Dispose();
        }



        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
                if (encoder.MimeType == mimeType)
                    return encoder;
            throw new ArgumentOutOfRangeException(
                string.Format("'{0}' not supported", mimeType));
        }


        ///  <summary>
        /// Required, but not used
        /// </summary>
        /// <returns>true</returns>
        public static bool ThumbnailCallback()
        {
            return true;
        }

        public static string[] ImgList (string pathTo)
        {
            if (!Directory.Exists(pathTo)) return new string[0];
            return Directory.GetFileSystemEntries(pathTo);
        }


    }
}

